package com.xu.cloud.common.lock;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

/**
 * Redisson锁实现
 *
 * @author xuguan
 * @since 2025/3/20
 */
public class RedissonDistributeLock implements IDistributeLock {
    private final RedissonClient redissonClient;
    private RLock lock;

    public RedissonDistributeLock(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public void lock(String lockName) {
        lock = redissonClient.getLock(lockName);
        lock.lock();
    }

    @Override
    public boolean tryLock(String lockName) {
        lock = redissonClient.getLock(lockName);
        return lock.tryLock();
    }

    @Override
    public void unlock() {
        lock.unlock();
    }
}
